<?php
/**
 * Created by PhpStorm.
 * User: envision
 * Date: 2018-09-28
 * Time: 9:12
 */

namespace App\Http\Controllers\Api\V1\Manager;


use App\Http\Controllers\Controller;
use App\Models\Answer;
use App\Models\Recommend;
use App\Models\TeacherLogin;
use App\Models\Test;
use App\Models\User;
use App\Models\UserCourse;
use App\Models\UserJourney;
use App\Models\UserStudent;
use App\Models\UserTeacher;
use App\Models\UserTest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class UserController extends Controller
{
    public function index(Request $request)
    {

        $pageSize = $request->pageSize ? $request->pageSize : 20;
        $major = $request->major;
        $userName = $request->userName;
        $startTime = $request->startTime;
        $endTime = $request->endTime;
        $sex = $request->sex;
        $statusTest = $request->statusTest;
        $userType = $request->userType;
        $sql = User::select();

        if ($major) {
            $sql->where('major',$major);
        }
        if ($userName) {
            $sql->where('userName',$userName);
        }
        if ($startTime) {
            $sql->where('createTime','>',$startTime);
        }
        if ($endTime) {
            $sql->where('createTime','<',date("Y-m-d", strtotime($endTime." +1 days")));
        }
        if ($sex) {
            $sql->where('sex',$sex);
        }
        if ($statusTest) {
            $sql->where('statusTest',$statusTest);
        }
        if($userType){
            $sql->where('userType',$userType);
        }
        $data = $sql->orderBy('createTime', 'desc')->paginate($pageSize)->toArray();
        foreach ($data['data'] as $k=>$v){
            if ($v['userType'] == 1){
                $teacher = UserTeacher::where('userId',$v['userId'])->first();
                $data['data'][$k]['teacherId'] = $teacher['teacherId'];
            }
        }
        $result['userList'] = $data['data'];
        $result['totalNum'] = $data['total'];
        return $this->apiResponse($result);
    }

    public function show($userId)
    {
        $data = User::find($userId);
        if ($data['userType'] == 1){
            $teacher = UserTeacher::where('userId',$userId)->first();
            $result['teacher'] = $teacher;
        }else{
            $student = UserStudent::where('userId',$userId)->first();
            $result['student'] = $student;
        }
        $result['data'] = $data;
        return $this->apiResponse($result);
    }

    public function store(Request $request)
    {
        $user = $request->user;
        $teacher = $request->teacher;
        $student = $request->student;
        $userId = $user['userId'];
        unset($user['userId']);
        if($userId){
            $status = User::where('userId',$userId)->update($user);
            if(!$status){
                return $this->apiResponse('', config('errorCode.NOT_UPDATE'));
            }
            if ($user['userType'] == 1){
                UserTeacher::where('userId',$userId)->update($teacher);
            }else{
                UserStudent::where('userId',$userId)->update($student);
            }
        }else{
            $data['createTime'] = date('Y-m-d');
            $status = User::create($data);
        }
        if(!$status){
            return $this->apiResponse('', config('errorCode.UPDATE_FAILED'));
        }
        return $this->apiResponse('');
    }

    public function destroy($userId)
    {
        User::destroy($userId);
        return $this->apiResponse('');
    }

    public function editStatus(Request $request)
    {
        $userId = $request->userId;
        $judge = $request->judge;

        if ($judge == 1){
            $status = User::where('userId', $userId)->update(['statusTest' => 2]);
            $user = User::find($userId);
            $teacher = UserTeacher::where('userId',$userId)->first(['teacherId']);
            $data['teacherId'] = $teacher['teacherId'];
            $data['account'] = $user['tel'];
            $data['password'] = substr($user['tel'],-6);
            TeacherLogin::create($data);
        }else{
            $status = User::where('userId', $userId)->update(['statusTest' => 3]);
        }
        if (!$status) {
            return $this->apiResponse('', config('errorCode.UPDATE_FAILED'));
        } else {
            return $this->apiResponse(true);
        }
    }

    public function courseUserList(Request $request)
    {
        $pageSize = $request->pageSize ? $request->pageSize : 20;

        $courseId = $request->courseId;
        $data = UserCourse::select()
            ->from((new User())->getTable() . ' as t1')
            ->join((new UserCourse())->getTable().' as t2','t2.userId','=','t1.userId')
            ->where('courseId',$courseId)->paginate($pageSize)->toArray();
        $result['userList'] = $data['data'];
        $result['totalNum'] = $data['total'];
        return $this->apiResponse($result);

    }

    public function onceTestAnswer(Request $request)
    {
        $userId = $request->userId;
        $user = UserTest::where('userId',$userId)->get()->toArray();
        $data = [];
        $development= Recommend::where('userId',$userId)->first();

        $data['userDevelopment']['major'] = json_decode($development['major']);
        $data['userDevelopment']['development'] = json_decode($development['development']);
        foreach ($user as $k=>$v){
            $data['list'][$k] = Test::find($v['testId']);
            $answer = Answer::where('testId',$v['testId'])->get()->toArray();
            $data['list'][$k]['answerList'] = $answer;
            $data['list'][$k]['userAnswer'] = $v['answerId'];
        }

        return $this->apiResponse($data);
    }
}